package com.lanou.myapp.funny.textfunny;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.lanou.myapp.R;
import com.lanou.myapp.circlepicture.Circlelmage;
import com.lanou.myapp.circlepicture.MyCircle;
import com.lanou.myapp.imageloader.VolleySingleton;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by dllo on 16/1/12.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
    private Context context;
    private List<ListData> data;



    public RecyclerAdapter(Context context, List<ListData> data) {
        this.context = context;
        this.data = data;

    }
//
//    private void init() {
//        queue = Volley.newRequestQueue(context);
//        lruCache = new LruCache<String, Bitmap>(20);
//        imageCache = new ImageLoader.ImageCache() {
//            @Override
//            public Bitmap getBitmap(String url) {
//                return lruCache.get(url);
//            }
//
//            @Override
//            public void putBitmap(String url, Bitmap bitmap) {
//                lruCache.put(url, bitmap);
//            }
//        };
//        //构建imageLoader对象
//        imageLoader = new ImageLoader(queue, imageCache);
//    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_text_funny, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
//        String imgurl = data.get(position).getAuthor().getHeader().get(0);
//        Log.d("iamgh", imgurl + "");
//        getBitmapCache(imgurl, holder.image);

        ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.image, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
        VolleySingleton.getImageLoader().get(data.get(position).getAuthor().getHeader().get(0), listener,200,200);

//        Glide.with(context).load(data.get(position).getAuthor().getHeader().get(0)).into(holder.image);


        holder.tv_name.setText(data.get(position).getAuthor().getName());
        holder.tv_time.setText(data.get(position).getPasstime());
        holder.tv_text.setText(data.get(position).getText());
        holder.tv_tag.setText(data.get(position).getTags().get(0).getName());
        if (data.get(position).getTags().size() > 1) {
            holder.tv_tag2.setText(data.get(position).getTags().get(1).getName());
        } else {
            holder.tv_tag3.setText("");

        }

        if (data.get(position).getTags().size() > 2) {
            holder.tv_tag3.setText(data.get(position).getTags().get(2).getName());
        } else {
            holder.tv_tag3.setText("");

        }

    }

    @Override
    public int getItemCount() {
        return data.size()/2;
    }


//    public void getBitmapCache(String imgUrl, ImageView imageView) {
//        // 构建图片监听器
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.v6_boy_unseletor_icon,
//                R.mipmap.v6_boy_unseletor_icon);
//        // 图片拉取
//        imageLoader.get(imgUrl, listener, 300, 300);
//    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        private CircleImageView image;
        private ImageView iv_tag;
        private TextView tv_name, tv_time, tv_text, tv_tag, tv_tag2, tv_tag3;

        public MyViewHolder(View itemView) {
            super(itemView);
            image = (CircleImageView) itemView.findViewById(R.id.iv_item_text_funny);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name_text_funny);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time_text_funny);
            tv_text = (TextView) itemView.findViewById(R.id.tv_title_text_funny);
            tv_tag = (TextView) itemView.findViewById(R.id.tv_tag_text_funny);
            tv_tag2 = (TextView) itemView.findViewById(R.id.tv_tag2_text_funny);
            tv_tag3 = (TextView) itemView.findViewById(R.id.tv_tag3_text_funny);

        }
    }

}
